<?php
$isReloadable = false;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');

reqDef('data_iniz', date('d/m/Y'));
reqDef('data_fine', date('d/m/Y', time() + 31536000));

foreach ($aTipiAttRint as $key => $value) {
	reqDef('tr_'.$key, 'X');
}	    			

if (reqVar('prefile')=='') $_REQUEST['prefile'] = uniqid('pref');

// array clienti.
$aCl = array();
$aCl[0] = ' - scegliere un cliente - '; 
foreach ($aClientiRint as $key => $value) {
	$aCl[$key] = $value['nome'];
}
asort($aCl);

// array responsabili e array richiedenti.
$aResp = array();
$aResp[0] = "- scegliere un responsabile - ";
$aRic = array();
foreach ($aUtentiRint as $key => $value) {
	$aResp[$key] = $value['nome'];
	if ($value['ric_ope'] == 'ric') {
		$aRic[$key]  = $value['nome'];
	}
}
asort($aResp);
asort($aRic);

//Gestione azioni
if (reqvar('azione')=='salva'){$msgPag .= confComm($aTipiAttRint, $aRic, $aCl);}

// array tipo rilevazione.
$aTipoRilevazione = array();
$aTipoRilevazione['R'] = 'Rint';
$aTipoRilevazione['A'] = 'Attivita\'';
$aTipoRilevazione['X'] = 'Entrambi';

$sCmdL = "";

$sCmdR = "";
$sCmdR .= "<a class='cmd1' href='#' onclick=\"submAzione('salva');\">conferma</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp";
$sCmdR .= cmdTorna('annulla');

testaHtml('Nuova commessa', $msgPag);

// Campi nascosti
echo getHtmlForm('frm1');
echo getHtmlHidden('azione','...');
echo getHtmlHidden('prefile');
echo getHtmlHidden('xsn',XSN);
?>

<script>
	function submAzione(azione) {
		document.frm1.azione.value=azione;
		document.frm1.submit();
	}
</script>

<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<tr>
		<td class="cmd1" align="left" valign='top'><?=$sCmdL?></td>
		<td class="cmd1" align="right"><?=$sCmdR?></td>
	</tr>
</table>

<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?=getHtmlRigaRif();?>
	<tr>
		<td class="tit1" colspan='20' align='center'>Nuova Commessa</td>
	</tr>
    <tr>
   	<td class="lab1" colspan='3'>Codice</td>
	  	<td class="txt1" colspan='7'><?=getHtmlText('cod_comm',40,32);?></td>
   	<td class="lab1" colspan='3'>Cod. Co.Ge.</td>
	  	<td class="txt1" colspan='7'><?=getHtmlText('cod_coge',10,5);?></td>
    </tr>
    <tr>
   	  	<td class="lab1" colspan='3'>Descrizione</td>
	  	<td class="txt1" colspan='17'><?=getHtmlText('descr',128,80);?></td>
    </tr>
    <tr>
      	<td class="lab1" colspan='3'>Data Inizio</td>
	  	<td class="txt1" colspan='7'><?=getHtmlDate('data_iniz');?></td>
      	<td class="lab1" colspan='3'>Data Fine</td>
	  	<td class="txt1" colspan='7'><?=getHtmlDate('data_fine');?></td>   
    </tr>
    <tr>	  
      	<td class="lab1" colspan='3'>Responsabile</td>
	  	<td class="txt1" colspan='7'><?=getHtmlSelect('id_responsabile',$aResp);?></td>
      	<td class="lab1" colspan='3'>Cliente no rint</td>
	  	<td class="txt1" colspan='7'><?=getHtmlSelect('id_cliente',$aCl);?></td>	  	  
    </tr>
    <tr>
    	<td class="lab1" colspan='3'>Tipo attivita'</td>
    	<td class="txt1" colspan='17'>
    	<?php
			if (count($aTipiAttRint) > 0) {
    			echo  "<table class='tab1' border='0' cellpadding='0' cellspacing='0'>";
   
	    		foreach ($aTipiAttRint as $key => $value) {
	    			echo "<tr>";
	    			echo "<td class='txt1'>".getHtmlCheckBox('ta_'.$key, $key)." ".$value['des']."</td>";
	    			echo "<td class=''txt1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
	    			echo "<td class='txt1'>".getHtmlRadio('tr_'.$key, $aTipoRilevazione)."</td>";
	    		    echo "</tr>";
	    		}

	    		echo "</table>";
			}
    	?>    	
    	</td>
    </tr>
    <tr>
    	<td class="lab1" colspan="3">Richiedenti</td>
    	<td class="txt1" colspan="7" valign="top"> 
    	<?php
			if (count($aRic) > 0) {
    			echo  "<table class='tab1' border='0' cellpadding='0' cellspacing='0'>";
   
	    		foreach ($aRic as $key => $desc) {
	    			echo "<tr>";
	    			echo "<td class='txt1'>".getHtmlCheckBox('rc_'.$key, $key)." ".$desc."</td>";
	    		    echo "</tr>";
	    		}
	    		
	    		echo "</table>";
			}
    	?>
    	</td>
    	<td class="lab1" colspan="3">Clienti rint</td>
    	<td class="txt1" colspan="7" valign="top">
    	<?php
			if (count($aCl) > 1) {
    			echo  "<table class='tab1' border='0' cellpadding='0' cellspacing='0'>";
   
	    		foreach ($aCl as $key => $desc) {
	    			if ($key == 0) {continue;}
	    			echo "<tr>";
	    			echo "<td class='txt1'>".getHtmlCheckBox('cl_'.$key, $key)." ".$desc."</td>";
	    		    echo "</tr>";
	    		}
	    		
	    		echo "</table>";
			}
    	?>
    	</td>    	
    </tr>
</table>

<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<tr>
		<td class="cmd1" align="left" valign='top'><?=$sCmdL?></td>
		<td class="cmd1" align="right"><?=$sCmdR?></td>
	</tr>
</table>
<?

echo "</form>";

codaHtml();
exit();

function confComm($aTipiAttRint, $aRic, $aCl) {
	global $objDB, $objSess;
	
	$msg = '';

	$_REQUEST['cod_comm'] = strtoupper(trim(reqVar('cod_comm')));
	$_REQUEST['cod_coge'] = strtoupper(trim(reqVar('cod_coge')));
	if (trim(reqVar('cod_comm'))=="") {
		$msg .= "codice obbligatorio<br>";
	} else {
		// verifica unicita' cod. commessa.
		$strCodUnico = " SELECT id_comm FROM commessa WHERE cod_comm = '".trim(reqVar('cod_comm'))."'";
		$objDB->query($strCodUnico,'strCodUnico');
		$nRCod = $objDB->numRec('strCodUnico');
		if ($nRCod > 0) {$msg .= "codice gia' esistente<br>";}
	}	
	
	if (trim(reqVar('cod_coge'))=="") {$msg .= "cod. Co.Ge. obbligatorio<br>";}
	if (trim(reqVar('descr'))=="") {$msg .= "descrizione obbligatoria<br>";}
	
	$odx = new rint2Date();
	if ($odx->setDMY(reqVar('data_iniz'))) {
			$dataIniz = $odx->getYMD();
	} else {
			$msg .= "data inizio ".reqVar('data_iniz')." errata <br>";
	}
	
	if ($odx->setDMY(reqVar('data_fine'))) {
			$dataFine = $odx->getYMD();
	} else {
			$msg .= "data fine ".reqVar('data_fine')." errata <br>";
	}
		
	if (trim(reqVar('id_responsabile')) == '0') {$msg .= "responsabile obbligatorio<br>";}
	
	$clienteObbl = false;
	$clienteRintObbl = false;
	foreach ($aTipiAttRint as $key => $value) {
		if (reqVar('ta_'.$key) != '') {		
			if (reqVar('tr_'.$key) == 'X') {
				$clienteObbl = true;
				$clienteRintObbl = true;
			} else if (reqVar('tr_'.$key) == 'A') {
				$clienteObbl = true;
			} else {
				$clienteRintObbl = true;
			}		
		}	
	}
	
	if (trim(reqVar('id_cliente')) == '0' and $clienteObbl) {$msg .= "cliente obbligatorio<br>";}
	
	$ncl = 0;
	foreach ($_POST as $key => $value) {
		if (substr($key, 0, 2) != "cl") {continue;}
		$ncl ++;
	}
	
	if ($ncl == 0 and $clienteRintObbl) {$msg .= "scegliere almeno un cliente rint<br>";}
		
	if ($msg != '') {return $msg;}
	
	$sSqlIns = " INSERT INTO commessa SET ". 
			   "   cod_comm       = '".reqVarAS('cod_comm')."'".
			   ",     descr       = '".reqVarAS('descr')."'".
			   ",  cod_coge       = '".reqVarAS('cod_coge')."'".
			   ", data_iniz       = '".$dataIniz."'".
			   ", data_fine       = '".$dataFine."'".
	           ", id_cliente      = ".reqVar('id_cliente').
			   ", id_responsabile = ".reqVar('id_responsabile');	
	$objDB->exec($sSqlIns);
	$newComm = $objDB->lastInsertId();	
		
	foreach ($aTipiAttRint as $key => $value) {
		if (reqVar('ta_'.$key) != '') {
			$sSqlAttPrev = " INSERT INTO att_previste SET ".
		                   "       id_comm = ".$newComm." ".
		                   ",      id_rint = 0 ".
		                   ",     tipo_att = ".$key.
		                   ", tipo_rilevazione = '".reqVar('tr_'.$key)."' ".
						   ",        stima = 0";
			$objDB->exec($sSqlAttPrev);
		}	
	}

	foreach ($aRic as $key => $value) {
		if (reqVar('rc_'.$key) != '') {
			$sInsRic = " INSERT INTO richiedenti SET ".
		               "       id_comm = ".$newComm." ".
		               ", id_operatore = ".reqVar('rc_'.$key);
			$objDB->exec($sInsRic);
		}	
	}
	
	foreach ($aCl as $key => $value) {
		if ($aCl == 0) {continue;}
		if (reqVar('cl_'.$key) != '') {
			$sInsCli = " INSERT INTO clienti_comm SET ".
		               "       id_comm = ".$newComm." ".
		               ", id_cliente = ".reqVar('cl_'.$key);
			$objDB->exec($sInsCli);
		}	
	}
	
	header("location: ".R2_URL_ROOT."/app/comm/commList.php?xsn=".urlencode($_REQUEST['xsn']));
	exit();
}

?>



